package org.rsbot.script.methods;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

public class Antiban extends MethodProvider {

	Antiban(final MethodContext ctx) {
		super(ctx);
	}
		
	public boolean hoverPlayer() {
		RSPlayer player = null;
		RSPlayer[] validPlayers = methods.players.getAll();

		player = validPlayers[random(0, validPlayers.length - 1)];
		if(player != null) {
			try {
				String playerName = player.getName();
				String myPlayerName = methods.players.getMyPlayer().getName();
				if (playerName.equals(myPlayerName)) {
					return false;
				}
			} catch(NullPointerException e) {}
			try {
				RSTile targetLoc = player.getLocation();
				Point checkPlayer = methods.calc.tileToScreen(targetLoc);
				if(methods.calc.pointOnScreen(checkPlayer) && checkPlayer != null) {
					methods.mouse.click(checkPlayer, 5, 5, false);
				} else {
					return false;
				}
				return true;
			} catch (Exception ignored) {
			}
		}
		return false;
	}

	public boolean hoverObject() {
		try {
			examineRandomObject(5);
			sleep(random(50, 1000));
			int mousemoveAfter2 = random(0, 4);
			sleep(random(100, 800));
			if (mousemoveAfter2 == 1 && mousemoveAfter2 == 2) {
				methods.mouse.move(1, 1, 760, 500);
			}
		} catch (Exception ignored) {
		}
		return true;
	}

	public RSTile examineRandomObject(int scans) {
		RSTile start = methods.players.getMyPlayer().getLocation();
		ArrayList<RSTile> possibleTiles = new ArrayList<RSTile>();
		for(int h = 1; h < scans * scans; h += 2) {
			for(int i = 0; i < h; i++) {
				for(int j = 0; j < h; j++) {
					int offset = (h + 1)/2 - 1;
					if(i > 0 && i < h - 1) {
						j = h - 1;
					}
					RSTile tile = new RSTile(start.getX() - offset + i, start.getY() - offset + j);
					RSObject objectToList = methods.objects.getTopAt(tile);
					if(objectToList!= null && methods.calc.tileOnScreen(objectToList.getLocation())) {
						possibleTiles.add(objectToList.getLocation());
					}
				}
			}
		}
		if (possibleTiles.size() == 0) {
			return null;
		}
		if(possibleTiles.size() > 0 && possibleTiles != null) {
			final RSTile objectLoc = possibleTiles.get(random(0,possibleTiles.size()));
			Point objectPoint = methods.calc.tileToScreen(objectLoc);
			if(objectPoint != null) {
				try {
					methods.mouse.move(objectPoint);
					if(methods.menu.doAction("Examine")) {
					} else {
					}
					try {
						sleep(random(100,500));
					} catch (Exception ignored) {
					}
				} catch(NullPointerException ignored) {}
			}
		}
		return null;
	}	
	
	public void wiggleMouse() {
		if(!methods.calc.pointOnScreen(methods.mouse.getLocation())) {
            methods.mouse.move(random(0, methods.game.getWidth(), methods.game.getWidth()/2, 150), random(0, methods.game.getHeight(), methods.game.getHeight()/2, 150));
		}
        moveMouseRandomly(random(15, 300));		
	}
	
	public void camera() {
		switch(random(1,4)) {
		case 1:
			new Thread(new CameraInput(UDkeycodes[random(0,2)], random(0, 250), random(250, 2000))).start();
			new Thread(new CameraInput(LRkeycodes[random(0,2)], random(0, 250), random(250, 4000))).start();
			break;
		case 2:
			new Thread(new CameraInput(LRkeycodes[random(0,2)], random(0, 250), random(250, 4000))).start();
			new Thread(new CameraInput(UDkeycodes[random(0,2)], random(0, 250), random(250, 2000))).start();
			break;
		case 3:
			new Thread(new CameraInput(LRkeycodes[random(0,2)], random(0, 250), random(250, 4000))).start();
			break;
		case 4:
			new Thread(new CameraInput(UDkeycodes[random(0,2)], random(0, 250), random(250, 2000))).start();
			break;
		}
		
	}
	
	private void moveMouseRandomly(int maxDistance) {
        Point p = methods.mouse.getLocation();
        int x = random(Math.max(0, p.x - maxDistance), Math.min(p.x + maxDistance, methods.game.getWidth()));
        int y = random(Math.max(0, p.y - maxDistance), Math.min(p.y + maxDistance, methods.game.getHeight()));
        methods.mouse.move(x, y);
    }
	
	private final char[] UDkeycodes = {KeyEvent.VK_UP, KeyEvent.VK_DOWN};
    private final char[] LRkeycodes = {KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT};
    
    private class CameraInput implements Runnable {
        private int priorWait, holdWait;
        private char key;
        public CameraInput(char key, int priorWait, int holdWait) {
            this.key = key;
            this.priorWait = priorWait;
            this.holdWait = holdWait;
        }
        public void run() {
            try {
                Thread.sleep(priorWait);
                methods.keyboard.pressKey(key);
                Thread.sleep(holdWait);
                methods.keyboard.releaseKey(key);
            } catch(InterruptedException e) {}
        }
    }
}